import { Command } from "commander";
import fs from "fs";
import pkg from "../package.json";
import { oss_upload_dir } from "./oss";

const program = new Command();

program
    .name(pkg.name)
    .description(pkg.description)
    .version(pkg.version);

// program.command('split')
//     .description('Split a string into substrings and display as an array')
//     .argument('<string>', 'string to split')
//     .option('--first', 'display just the first substring')
//     .option('-s, --separator <char>', 'separator character', ',')
//     .action((str, options) => {
//         const limit = options.first ? 1 : undefined;
//         console.log(str.split(options.separator, limit));
//     });

program.command("oss_upload_dir")
    .description("上传文件夹到阿里云OSS")
    .option("-l, --local_dir <string>", "本地目录", "./public")
    .option("-o, --oss_dir <string>", "OSS目录", "")
    .action((option) =>
    {
        const local_dir = option.local_dir;
        let oss_dir = option.oss_dir;

        if (!fs.existsSync(local_dir))
        {
            console.log(`本地目录 ${local_dir} 不存在!`);

            return;
        }
        if (!oss_dir)
        {
            // 获取 当前目录下 package.json 的 name 字段
            const package_json = fs.readFileSync("package.json", "utf-8");
            const package_json_obj = JSON.parse(package_json);
            oss_dir = package_json_obj.name.split("/").pop();
        }
        console.log(`上传文件夹到阿里云OSS: ${local_dir} -> ${oss_dir}`);
        oss_upload_dir(local_dir, oss_dir);
    });

program.parse();